﻿using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PHMEE.CommonUtils;
using PHMEE.Localization;
using PHMEE.PHM.Algorithms;
using Volo.Abp.AuditLogging;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.FeatureManagement;
using Volo.Abp.Identity;
using Volo.Abp.Localization;
using Volo.Abp.Localization.ExceptionHandling;
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
using Volo.Abp.Validation.Localization;
using Volo.Abp.VirtualFileSystem;

namespace PHMEE;

[DependsOn(
    typeof(AbpAuditLoggingDomainSharedModule),
    typeof(AbpBackgroundJobsDomainSharedModule),
    typeof(AbpFeatureManagementDomainSharedModule),
    typeof(AbpIdentityDomainSharedModule),
    typeof(AbpOpenIddictDomainSharedModule),
    typeof(AbpPermissionManagementDomainSharedModule),
    typeof(AbpSettingManagementDomainSharedModule),
    typeof(AbpTenantManagementDomainSharedModule)    
    )]
public class PHMEEDomainSharedModule : AbpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        PHMEEGlobalFeatureConfigurator.Configure();
        PHMEEModuleExtensionConfigurator.Configure();
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        context.Services.PHMAlgorithmsConfigureServices(configuration);

        Configure<AbpVirtualFileSystemOptions>(options =>
        {
            options.FileSets.AddEmbedded<PHMEEDomainSharedModule>();
        });

        Configure<AbpLocalizationOptions>(options =>
        {
            options.Resources
                .Add<PHMEEResource>("en")
                .AddBaseTypes(typeof(AbpValidationResource))
                .AddVirtualJson("/Localization/PHMEE");

            options.DefaultResourceType = typeof(PHMEEResource);
            
            // Diagnosis 故障诊断
            options.Resources
                .Add<DiagnosisResource>("en")
                .AddVirtualJson("/Localization/Diagnosis");
                
            // EquipParameters 机组、设备属性
            options.Resources
                .Add<EquipParametersResource>("en")
                .AddVirtualJson("/Localization/EquipParameters");
                
            // EnergyEfficiency 能效 1.0 迁移过来的
            options.Resources
                .Add<EnergyEfficiencyResource>("en")
                .AddVirtualJson("/Localization/EnergyEfficiency");
        });

        Configure<AbpExceptionLocalizationOptions>(options =>
        {
            options.MapCodeNamespace("PHMEE", typeof(PHMEEResource));
        });
        
        context.Services.PHMAlgorithmsConfigureServices(context.Services.GetConfiguration());
        
        // 注册全局 TimeProvider，方便单元测试
        context.Services.AddSingleton(TimeProvider.System);
        context.Services.AddSingleton<IPHMEETimeProvider, PHMEETimeProvider>();
    }
}
